package com.example.demo.controller;

import com.example.demo.common.LoginSession;
import com.example.demo.model.ArticleInfo;
import com.example.demo.model.UserInfo;
import com.example.demo.service.ArticleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 文章控制器
 */

@RestController
@RequestMapping("/art")
public class ArticleController {

    @Autowired
    private ArticleService articleService;

    /**
     * 获取个人列表信息(文章..博客.....)
     * @param request
     * @return
     */
    @RequestMapping("/list")
    public List<ArticleInfo> getList(HttpServletRequest request) {
        UserInfo userinfo = LoginSession.getSessionUserInfo(request);
        if(userinfo!=null) {
            return articleService.getList(userinfo.getId());
        }
        return null;
    }

    /**
     * 获取正文信息
     */
    @RequestMapping("/detail")
    public ArticleInfo getDetail(Integer aid) {
        //uid不合法判断
        if(aid==null || aid<=0) {
            return null;
        }
        ArticleInfo articleInfo = articleService.getArticleInfo(aid);
        if(articleInfo!=null) {
            return articleInfo;
        }
        return null;
    }
    /**
     * 通过id查询文章详情并展现到Markdown编译器
     */
    @RequestMapping("/detailbyid")
    public ArticleInfo detailById(HttpServletRequest request,Integer aid) {
        if(aid==null || aid<=0) {
            return null;
        }
        /**
         * 通过session得到当前登陆用户
         */
        UserInfo userinfo = LoginSession.getSessionUserInfo(request);
        if(userinfo!=null) {
            ArticleInfo articleInfo = articleService.getArticleInfo(aid);
            //进行身份校验
            if(articleInfo!=null && userinfo.getId()==articleInfo.getUid()) {
                return articleInfo;
            }
        }
        return null;
    }

}